<?php

	mb_internal_encoding('UTF-8');

	// CLI
	if (PHP_SAPI == 'cli') {
		ini_set('max_execution_time', '0');

		if (!empty($argv)) {
			unset($argv[0]);
			foreach ($argv as $param) {
				if (mb_substr($param, 0, 2) != '--') continue;
				$param = mb_substr($param, 2);
				list($key, $value) = explode('=', $param, 2);
				$_REQUEST[$key] = $value;
			}
		}
	}

//	set_error_handler(function ($errno = 0, $errstr = '', $errfile = '', $errline = 0, $errcontext = array()) {
//		if (mb_strpos($errstr, 'mysql_connect(): Headers and client library minor version mismatch.') !== false) return true;
//
//		$db = Database::getInstance();
//
//		$db->insertTable('error', array(
//			'errno'      => $errno,
//			'errstr'     => $errstr,
//			'errfile'    => $errfile,
//			'errline'    => $errline,
//			'errcontext' => json_encode($errcontext),
//			'request'    => http_build_query($_REQUEST),
//			'date'       => date('Y-m-d H:i:s'),
//		));
//
//		return false;
//	});

	// CLASS AUTOLOADER
	spl_autoload_register(function ($className) {
		if (preg_match('/[^\p{Common}\p{Latin}]/u', $className)) {
			throw new AppException('Security error', System_Errors::ERROR_SYSTEM_SECURITY);
		}

		$route = explode('_', $className);
		switch ($route[0]) {
			case 'System':
				$classPath = SYSTEM_PATH . '/' . implode('/', array_slice($route, 1)) . '.php';
				if (!file_exists($classPath)) {
					throw new AppException('System module "' . $className . '" not found.', System_Errors::ERROR_SYSTEM_MODULE_NOT_FOUND);
				}
				break;
			case 'Model':
				$classPath = MODEL_PATH . '/' . implode('/', array_slice($route, 1)) . '.php';
				if (!file_exists($classPath)) {
					throw new AppException('Model "' . $className . '" not found.', System_Errors::ERROR_MODEL_NOT_FOUND);
				}
				break;
			case 'Module':
				$classPath = MODULE_PATH . '/' . implode('/', array_slice($route, 1)) . '.php';
				if (!file_exists($classPath)) {
					throw new AppException('Module "' . $className . '" not found.', System_Errors::ERROR_MODULE_NOT_FOUND);
				}
				break;
			case 'Config':
				$classPath = CONFIG_PATH . '/' . implode('/', array_slice($route, 1)) . '.php';
				if (!file_exists($classPath)) {
					throw new AppException('Config "' . $className . '" not found.', System_Errors::ERROR_CONFIG_NOT_FOUND);
				}
				break;
			case 'Interface':
				$classPath = INT_PATH . '/' . implode('/', array_slice($route, 1)) . '.php';
				if (!file_exists($classPath)) {
					throw new AppException('Interface "' . $className . '" not found.', System_Errors::ERROR_INTERFACE_NOT_FOUND);
				}
				break;
			default:
				$classPath = ($route[0][0] == 'O' ? OBJ_PATH : LIB_PATH) . '/' . implode('/', $route) . '.php';
		}

		if (!file_exists($classPath)) {
			if ($route[0][0] == 'O') {
				$classPath = LIB_PATH . '/' . implode('/', $route) . '.php';
				if (!file_exists($classPath)) {
					throw new AppException('Class "' . $className . '" not found in "' . $classPath . '".', System_Errors::ERROR_CLASS_NOT_FOUND);
				}
			} else {
				throw new AppException('Class "' . $className . '" not found in "' . $classPath . '".', System_Errors::ERROR_CLASS_NOT_FOUND);
			}
		}

		require_once($classPath);
	});

